clear all
set more off, perm
set mem 10000000
set matsize 10000
version 15

**************************************************************
** RDROBUST placebo test for 2011 VD electricitiy variables **
**************************************************************

** Set file paths
do "$path_code/paths.do"

**************************************************************
**************************************************************

** Estimate 10000 RDs with 150-person bandwidths, with thresholds picked between 150 and 1000
use "$sens/RDROBUST_placebo_test_vdelec.dta", clear
keep if tot_p < 1500
local controls = "lights_max1998 lights_max1999 lights_max2000 lights_max2001 lights_max2002 lights_max2003 lights_max2004 lights_max2005" 
local kernel = "tri"
local bwmethod = "mserd"
local vce = ""
local poly = 1

	// Set random seed and ensure sufficient observations
set seed 91784
gen row = _n
if _N<10000 {
	set obs 10000
}

	// Generate placebo cutoffs
gen placebo_low  = round(151+(275-151)*runiform(),1) if row<=10000
gen placebo_high = round(325+(1000-325)*runiform(),1) if row<=10000
gen placebo_hilo = (round(0+(801-0)*runiform(),1)<125) if row<=10000
gen placebo = placebo_hilo*placebo_low + (1-placebo_hilo)*placebo_high if row<=10000
replace placebo = placebo - 0.5
drop placebo_low placebo_high placebo_hilo

	// Create variables to store regression results
gen yvar = "vd_pwr_`y'_11"
gen ifs = "in_fs_sample"
gen control = "`controls'"
gen fe = ""
gen kernel = "`kernel'"
gen bwmethod = "`mserd'"
gen vce = "`vce'"
gen polynomial_order = `poly'
foreach y in d_com d_all h_com {
	gen `y'_fs_beta_robust = .
	gen `y'_fs_se_robust = .
	gen `y'_fs_pval_robust = .
	gen `y'_fs_lci_robust = .
	gen `y'_fs_uci_robust = .
	gen `y'_fs_bw_lo = .
	gen `y'_fs_bw_hi = .
	gen `y'_fs_nobs_orig = .
	gen `y'_fs_nobs_left = .
	gen `y'_fs_nobs_right = .
	gen `y'_fs_ymean = .
}
		
	// Store empty dataset of placebo results, to populate below
sort placebo
save "$sens/RDROBUST_placebo_test_vdelec_results.dta", replace
	
	
	// Run placebo RDROBUSTs
use "$sens/RDROBUST_placebo_test_vdelec_results.dta", clear 
forvalues i = 1/10000 {

	// Set placebo threshold 
	local PLACEBO = placebo[`i'] 
	
	// If it's a new placebo threshold, run RDs
	if `PLACEBO'>placebo[`i'-1] | `i'==1 {

		// Clear break flag
		local loop_flag = ""

		// Loop over outcomes variables 
		foreach y in d_com d_all h_com {
			
			local yvar = "vd_pwr_`y'_11"
			local ifs = "in_fs_sample==1"
			if "`y'"=="h_com" {
				local fe = "STFEH*"
			}
			else  {
				local fe = "STFE*"
			}

			// Clear ereturn list
			ereturn clear

			// Run placebo RD
			cap rdrobust `yvar' tot_p if `ifs', c(`PLACEBO') covs(`controls' `fe') kernel(`kernel') bwselect(`bwmethod') p(`poly') all `vce'
			
			// Flag iterations that break rdrobust
			if e(tau_bc)==. {
				local loop_flag = "  rdrobust broke!"
			}
			
			// Generate in-sample indicator
			cap drop temp_in_reg
			cap gen temp_in_reg = inrange(tot_p,`PLACEBO'-e(h_l),`PLACEBO'+e(h_r))
			cap sum `yvar' if temp_in_reg==1 & tot_p<`PLACEBO'
			local r_mean = r(mean)
			
			// Store results
			cap replace `y'_fs_beta_robust = e(tau_bc) if _n==`i'
			cap replace `y'_fs_se_robust = e(se_tau_rb) if _n==`i'
			cap replace `y'_fs_pval_robust = e(pv_rb) if _n==`i'
			cap replace `y'_fs_lci_robust = e(ci_l_rb) if _n==`i'
			cap replace `y'_fs_uci_robust = e(ci_r_rb) if _n==`i'
			cap replace `y'_fs_bw_lo = e(h_l) if _n==`i'
			cap replace `y'_fs_bw_hi = e(h_r) if _n==`i'
			cap replace `y'_fs_nobs_orig = e(N) if _n==`i'
			cap replace `y'_fs_nobs_left = e(N_h_l) if _n==`i'
			cap replace `y'_fs_nobs_right = e(N_h_r) if _n==`i'
			cap replace `y'_fs_ymean = `r_mean' if _n==`i'
			
		}
	}
	
	// If it's a repeat placebo threshoold, carry forward results 
	else if `PLACEBO'==placebo[`i'-1] {
		
		// Loop over outcomes variables 
		foreach y in d_com d_all h_com {
			
			// Store results
			foreach v of varlist `y'_fs_* {
				cap replace `v' = `v'[_n-1] if _n==`i'
			}
		}
	}

	// Save updated dataset
	qui save "$sens/RDROBUST_placebo_test_vdelec_results.dta", replace

	// Report intermediate output
	di "`i'  `loop_flag'"
}


keep placebo yvar-h_com_fs_ymean
keep in 1/10000
compress
save "$sens/RDROBUST_placebo_test_vdelec_results.dta", replace

**************************************************************
**************************************************************
